package com.sskj.mine;

import android.text.TextUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.MD5Util;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.util.CommonUtil;
import com.sskj.mine.bean.ImgHighBean;
import com.sskj.mine.bean.PayWayBean;
import com.sskj.mine.bean.PayWayItem;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.BasePresenter;
import com.sskj.mine.SecurityActivity;

import java.io.File;
import java.util.List;


/**
 * @author Hey
 * Create at  2019/05/01
 */
class SecurityPresenter extends BasePresenter<SecurityActivity> {

    public void requestUserInfo() {
        OkGo.<HttpData<UserData>>get(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .cacheKey(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new JsonCallBack<HttpData<UserData>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<UserData>> response) {
                        HttpData<UserData> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateStatus(httpData.getData());
                        }
                    }
                });
    }

    public void cancelShop() {
        OkGo.<HttpData<Object>>get(HttpConfig.BASE_URL + HttpConfig.SHOPCANCEL)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        HttpData<Object> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            ToastUtil.showShort(httpData.getMsg());
                            mView.cancelShopSuccess();
                            requestUserInfo();
                        }
                    }
                });
    }

    // 获取支付方式列表
    public void requsetPayWayList() {
        OkGo.<HttpData<List<PayWayItem>>>post(HttpConfig.BASE_URL + HttpConfig.PAY_WAY_LIST)
                .execute(new JsonCallBack<HttpData<List<PayWayItem>>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<List<PayWayItem>>> response) {
                        mView.updateUi(response.body());
                    }
                });
    }


    // 解除谷歌认证
    public void toggleGoogleVerify(int googleType, String code, String mobileCode, String mobile) {
        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.SET_GOOGLE_VERIFY)
                .params("act", googleType)
                .params("dyGoodleCommand", code)
                .params("code", mobileCode)
                .params("mobile", mobile)
                .execute(new JsonCallBack<HttpData<Object>>() {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        mView.setGoogleVerifySuccess();
                    }
                });
    }

    // 获取邮箱验证码
    public void sendEmail(String email) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SEND_EMAIL)
                .params("email", email)
                .execute(new JsonCallBack<HttpData>(this) {

                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        mView.onAuthCodeSuccess();
                    }
                });
    }

    // 获取短信验证码
    public void requestAuthCode(String account) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SMS_SEND)
                .params("mobile", account)
                .params("type", 7)
                .execute(new JsonCallBack<HttpData>(this, true) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        ToastUtil.showShort(httpListData.getMsg());
                        if (httpListData.getStatus() == 200) {
                            mView.onAuthCodeSuccess();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData> response) {
                        super.onError(response);
                    }
                });

    }

    //上传图片
    public void submitVerify(File file) {
        OkGo.<HttpData<ImgHighBean>>post(HttpConfig.BASE_URL + HttpConfig.HIGH_VERIFY_IMG)
                .isMultipart(false)
                .params("file", file)
                .execute(new JsonCallBack<HttpData<ImgHighBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<ImgHighBean>> response) {
                        HttpData body = response.body();
                        if (body.status == HttpConfig.OK) {
                            ToastUtil.showShort(body.msg);
                            ImgHighBean hignBean = (ImgHighBean) body.getData();
                            mView.putList(hignBean.getUrl());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<ImgHighBean>> response) {
                        super.onError(response);
                        HttpData body = response.body();
                        mView.uploadFail();
                        if (body != null && body.msg != null) {
                            ToastUtil.showShort(body.msg);
                        } else {
                            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityPresenter1_j));
                        }

                    }
                });
    }

    // 添加修改支付方式
    public void requestAddPayWay(String type, String selectName, String bankName, String bankBranch, String bankNum, String aliaccount, String imagePath, String payPwd) {

        HttpParams params = new HttpParams();
        if (!TextUtils.isEmpty(imagePath)) {
            params.put("ali_img", imagePath);
            params.put("wx_img", imagePath);
//            File file = new File(imagePath);
//            params.put("wx_img", file);
//            params.put("ali_img ", file);
        }

        params.put("act", type);
        params.put("type", selectName);
        params.put("bank", bankName);
        params.put("branch", bankBranch);
        params.put("bank_car", bankNum);
        params.put("wx", aliaccount);
        params.put("alipay", aliaccount);
        params.put("pal_account", aliaccount);
        params.put("tpwd", CommonUtil.getFiveMd5Value(payPwd));
        ;
        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.ADD_PAY_PWD)
                .params(params)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == 200) {
                            requsetPayWayList();
                            mView.onChangePayWaySuccess();
                        }
                    }
                });
    }

    // 添加修改支付方式
    public void requestAddPayWay2(String act, String type, String bank, String name, String bankcode, String bankname, String pwd) {
        HttpParams params = new HttpParams();
        params.put("act", act);
        params.put("type", type);
        params.put("bank", bank);
        params.put("branch", bankname);
        params.put("bank_car", bankcode);
        params.put("tpwd", CommonUtil.getFiveMd5Value(pwd));
        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.ADD_PAY_PWD)
                .params(params)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == 200) {
                            requsetPayWayList();
                            mView.onChangePayWaySuccess();
                        }
                    }
                });
    }

    // 支付方式开关
    public void requsetCheckSwitch(PayWayItem item) {
        int status = item.getStatus() == 0 ? 1 : 0;
        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.CHANGE_SWITCH)
                .params("type", item.getType())
                .params("status", status)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        item.setStatus(status);
                        mView.onChangeSwitchSuccess(item);
                    }
                });
    }

    // 申请商家
    public void shopVerify() {
        OkGo.<HttpData<Object>>get(HttpConfig.BASE_URL + HttpConfig.SHOPVERIFY)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        HttpData<Object> httpData = response.body();

                        if (httpData.getStatus() == HttpConfig.OK) {
                            ToastUtil.showShort(httpData.getMsg());
                            mView.dismissShow();
                        }
                    }
                });
    }

}
